﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data;

namespace WirtualneNauczanie.Web
{
    
    public partial class LoggedIn : System.Web.UI.Page
    {
        DropDownList DropDownList1;
        protected void Page_Load(object sender, EventArgs e)
        {
            
           /* 
            //Pobranie ID usera
            MembershipUser myObject = Membership.GetUser();
            string StudentID = myObject.ProviderUserKey.ToString();

            //Kwerenda pobierajaca wszystkie kursy do ktorych nalezy uzytkownik
            SqlDataSource1.SelectCommand =
                "SELECT Courses.CourseID, Courses.CourseName FROM Courses INNER JOIN StudentsCourses ON Courses.CourseID = StudentsCourses.CourseID WHERE     (StudentsCourses.StudentID = '" + StudentID + "')";
            
            DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
            int numOfRows = dv.Table.Rows.Count;
            if (numOfRows > 0) { Label2.Text = "Twoje kursy"; }
            else { Label2.Text = "Nie jesteś zapisany do żadnego kursu nauczania"; }

            DropDownList1 = new DropDownList();
            DropDownList1.DataSource = SqlDataSource1;
            DropDownList1.DataTextField = "CourseName";
            DropDownList1.DataValueField = "CourseID";            
            DropDownList1.DataBind();

            PlaceHolder1.Controls.Add(DropDownList1);
            */
            kw3();
        }


        private void kw3()
        {
            DataClasses1DataContext db = new DataClasses1DataContext();

            //Pobranie ID usera
            MembershipUser myObject = Membership.GetUser();
            string StudentID = myObject.ProviderUserKey.ToString();

            var query = from sc in db.StudentsCourses
                        join c in db.Courses on sc.CourseID equals c.CourseID
                        where sc.StudentID.Equals(StudentID)
                        select new
                        {
                            CourseID = c.CourseID,
                            CourseName = c.CourseName,
                            CourseDescription = c.CourseDescription
                        };

            DropDownList1 = new DropDownList();
            foreach (var course in query)
            {
                String id = Convert.ToString(course.CourseID);
                String name = course.CourseName;                
                DropDownList1.Items.Add(new ListItem(name, id));

            }
            PlaceHolder1.Controls.Add(DropDownList1);
        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            //Pobranie ID wybranego kursu
            //String StudentCourseID =  DropDownList1.SelectedValue //DropDownList1.SelectedValue;
            String CourseID = DropDownList1.SelectedItem.Value;
            
            Response.Redirect("Course.aspx?yourcourseid="+CourseID);
        }
    }
}
